package com.ruoyi.rsikManage.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.GenericMethodsService;
import com.ruoyi.common.core.domain.entity.SysDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rsikManage.domain.TUnitConfig;
import com.ruoyi.rsikManage.service.ITUnitConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统平台配置信息Controller
 *
 * @author sxygsj
 * @date 2024-10-23
 */
@RestController
@RequestMapping("/riskManage/config")
public class TUnitConfigController extends BaseController
{
    @Autowired
    private ITUnitConfigService tUnitConfigService;

    @Autowired
    private GenericMethodsService genericMethodsService;

    /**
     * 查询系统平台配置信息列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:config:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody TUnitConfig tUnitConfig)
    {
        startPage();
        List<TUnitConfig> list = tUnitConfigService.selectTUnitConfigList(tUnitConfig);
        return getDataTable(list);
    }

    /**
     * 导出系统平台配置信息列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:config:export')")
    @Log(title = "系统平台配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUnitConfig tUnitConfig)
    {
        List<TUnitConfig> list = tUnitConfigService.selectTUnitConfigList(tUnitConfig);
        ExcelUtil<TUnitConfig> util = new ExcelUtil<TUnitConfig>(TUnitConfig.class);
        util.exportExcel(response, list, "系统平台配置信息数据");
    }

    /**
     * 获取系统平台配置信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tUnitConfigService.selectTUnitConfigById(id));
    }

    /**
     * 新增系统平台配置信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:config:add')")
    @Log(title = "系统平台配置信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUnitConfig tUnitConfig)
    {
        return toAjax(tUnitConfigService.insertTUnitConfig(tUnitConfig));
    }

    /**
     * 修改系统平台配置信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:config:edit')")
    @Log(title = "系统平台配置信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUnitConfig tUnitConfig)
    {
        return toAjax(tUnitConfigService.updateTUnitConfig(tUnitConfig));
    }

    /**
     * 删除系统平台配置信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:config:remove')")
    @Log(title = "系统平台配置信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tUnitConfigService.deleteTUnitConfigByIds(ids));
    }


    /**
     * 删除系统平台配置信息
     */
    @Log(title = "系统平台配置信息", businessType = BusinessType.DELETE)
    @GetMapping("/list/deptMap")
    public AjaxResult deptMap()
    {

        Map<Long, SysDept> deptMap = genericMethodsService.getDeptMap();
        // 将 Map 转换为 List
        List<SysDept> deptList = deptMap.values().stream().collect(Collectors.toList());
        List<Long> unitCodes = new ArrayList<>();
        for (SysDept dept:deptList){
            unitCodes.add(dept.getUnitCode());
            System.out.println(dept.getUnitCode());
            }
        return null;
    }

}

